package com.cloudeggtech.granite.sdk;

import com.cloudeggtech.chalk.core.stream.IAuthenticationFailure;
import com.cloudeggtech.chalk.core.stream.IAuthenticationToken;
import com.cloudeggtech.basalt.protocol.core.stream.sasl.Failure;

public class SaslAuthenticationFailure implements IAuthenticationFailure {

    private AndroidSaslNegotiant sasl;
    private Failure.ErrorCondition errorCondition;
    private boolean retriable;
    private int count;

    public SaslAuthenticationFailure(AndroidSaslNegotiant sasl, Failure.ErrorCondition errorCondition, boolean retriable, int count) {
        this.sasl = sasl;
        this.errorCondition = errorCondition;
        this.retriable = retriable;
        this.count = count;
    }

    @Override
    public boolean isRetriable() {
        return retriable;
    }

    @Override
    public Failure.ErrorCondition getErrorCondition() {
        return errorCondition;
    }

    @Override
    public int getFailureCount() {
        return count;
    }

    @Override
    public void retry(IAuthenticationToken authToken) {
        if (!retriable) {
            throw new IllegalStateException("authenticate failure isn't retriable");
        }

        sasl.retry(authToken);
    }

    @Override
    public void abort() {
        if (!retriable) {
            return;
        }

        sasl.abort();
    }
}
